interface PayLoad {
    type: string
    payload: {
      [key: string]: any
    }
  }

  export interface TodoList {
    id: string
    text: string
    completed: boolean
  }
  // 初始值
  const initState: TodoList[] = []
  const todos = (state = initState, action: PayLoad): TodoList[] => {
    switch (action.type) {
      // 新增
      case 'ADD_TODO':
        return [
          ...state,
          {
            id: action.payload.id, // key
            text: action.payload.text, // 内容
            completed: false // 未完成
          }
        ]
      // 切换完成状态
      case 'TOGGLE_TODO':
        return state.map(todo =>
          todo.id === action.payload.id
            ? { ...todo, completed: !todo.completed } // 当前元素变化
            : todo
        )
      case 'todos/fetchTodosSuccess': // 请求成功
        console.log(action.payload)
        return state
        case 'todos/fetchTodosFailure': // 请求失败
          console.error(action.payload)
          return state
      default:
        return state
    }
  }
  export default todos
